package com.example.projecth.project3;

import com.example.projecth.R;

import android.app.Fragment;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

public class StopWatchFragment extends Fragment {

	private TextView time;
	private CountDownTimer timer;
	private Button[] button;
	private TextView textView;
	private ScrollView scroll;
	private int value = -1;
	private int secValue = 0;
	private int minValue = 0;
	private int hourValue = 0;
	private int click = 0;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.stopwatch_fragment, container, false);
		time = (TextView) view.findViewById(R.id.timer);
		textView = (TextView) view.findViewById(R.id.list);
		scroll = (ScrollView) view.findViewById(R.id.scroll);

		button = new Button[3];
		button[0] = (Button) view.findViewById(R.id.start);
		button[1] = (Button) view.findViewById(R.id.lap);
		button[2] = (Button) view.findViewById(R.id.reset);

		// scroll.invalidate();
		// scroll.requestLayout();

		timer = new CountDownTimer(1000000 * 1000, 10) {

			@Override
			public void onTick(long millisUntilFinished) {

				value++;
				time.setText(String.format("%02d:%02d:%02d:%02d", hourValue,
						minValue, secValue, value));

				if (value == 100) {
					secValue++;
					value = 0;
					time.setText(String.format("%02d:%02d:%02d:%02d",
							hourValue, minValue, secValue, value));
				}
				if (secValue == 60) {
					minValue++;
					secValue = 0;
					time.setText(String.format("%02d:%02d:%02d:%02d",
							hourValue, minValue, secValue, value));
				}
				if (minValue == 60) {
					hourValue++;
					minValue = 0;
					time.setText(String.format("%02d:%02d:%02d:%02d",
							hourValue, minValue, secValue, value));
				}

			}

			@Override
			public void onFinish() {
				timer.cancel();
				time.setText("00:00:00:00");

			}
		};

		for (int i = 0; i < button.length; i++) {
			button[i].setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					switch (v.getId()) {
					case R.id.start:
						if (button[0].getText().toString().equals("START")) {
							timer.start();
							button[0].setText("STOP");
						} else if (button[0].getText().toString()
								.equals("STOP")) {
							timer.cancel();
							button[0].setText("START");
						}
						break;
					case R.id.lap:
						if (button[0].getText().toString().equals("STOP")) {
							click++;
							textView.append(String.format("LAP%2d" + ". ", click)
									+ time.getText() + "\n");
							scroll.scrollTo(0, textView.getHeight());
//							scroll.post(new Runnable() {
//								public void run() {
//									// TODO Auto-generated method stub
//									scroll.scrollTo(0, textView.getHeight());
//								}
//							});
//							
						}
						break;
					case R.id.reset:
						textView.setText("");
						click = 0;
						value = 0;
						minValue = 0;
						secValue = 0;
						hourValue = 0;
						button[0].setText("START");
						timer.onFinish();
						break;

					}

				}
			});
		}

		return view;

	}

	@Override
	public void onPause() {
		timer.cancel();
		super.onPause();
	}

}
